#include "clienthandler.h"
#include "datamanger.h"
#include <QMessageBox>
#include <QFile>
#include <QFileInfo>

clientHandler::clientHandler(QTcpSocket *socket, QObject *parent) : QObject(parent)
{
    this->socket = socket;
    connect(socket,&QTcpSocket::readyRead,this,&clientHandler::onReadyRead);
}

void clientHandler::onReadyRead()
{
    while (socket->bytesAvailable() > 0)
    {
        messagePackage pack = messagePackage::recvMessage(socket);
        qDebug() << "type : " << pack.type();
        //处理请求
        switch (pack.type())
        {
        case messagePackage::Register:
            userRegister(pack.userName(), pack.userPassword());
            break;
        case messagePackage::Login:
            userLogin(pack.userName(), pack.userPassword());
            break;
        case messagePackage::OnlineUser:
            emit getOnlineUsers();
            break;
        case messagePackage::PrivateChat:
            emit privateChat(pack);
            break;
        case messagePackage::AllUsers:
            getAllUsers();
            break;
        case messagePackage::Group:
            createGroup(pack.allUsers(), pack.groupName(), pack.userName());
            break;
        case messagePackage::GroupChat:
            emit groupChat(pack);
            break;
        case messagePackage::InviteGroup:
            emit inviteGroup(pack);
            break;
        case messagePackage::UploadFile:
            uploadFile(pack);
            break;
        case messagePackage::AllFile:
            fileinfo();
            break;
        case messagePackage::DownloadFile:
            downlaodFile(pack.fileName());
            break;
        case messagePackage::GroupFile:
            groupFile(pack);
            break;
        case messagePackage::Meeting:
            emit meeting(pack);
            break;
        case messagePackage::InviteMeeting:
            emit inviteMeeting(pack);
            break;
        case messagePackage::UpdateMeeting:
            emit updateMeeting(pack);
            break;
        case messagePackage::QuitMeetintg:
            emit quitMeeting(pack);
            break;
        case messagePackage::ClearMeeting:
            emit clearMeeting(pack);
            break;
        }
    }
}

// 用户注册
void clientHandler::userRegister(QString name, QString pswd)
{
    messagePackage retMessage;
    retMessage.setType(messagePackage::Register);
    bool ret = datamanger::getInstance()->regist(name, pswd);
    retMessage.setRet(ret);
    retMessage.sendMessage(&retMessage, socket);

    if (ret)
    {   // 注册成功则将姓名添加进GroupInfo
        datamanger::getInstance()->alterGroupInfo(name);
    }
}

// 用户登录
void clientHandler::userLogin(QString name, QString pswd)
{
    messagePackage retMessage;
    retMessage.setType(messagePackage::Login);
    int ret = datamanger::getInstance()->login(name, pswd);
    retMessage.setRetInt(ret);

    retMessage.sendMessage(&retMessage, socket);
    if (ret == 1)
    {
        // 获取群聊列表
        messagePackage pack;
        pack.setType(messagePackage::InviteGroup);
        QStringList groups = datamanger::getInstance()->selectGroup(name);
        for (auto group : groups)
        {
            pack.setGroupName(group);
            messagePackage::sendMessage(&pack, socket);
        }

        emit userOnline(name, socket);
    }
}


// 获取所有用户名
void clientHandler::getAllUsers()
{
    QStringList users = datamanger::getInstance()->getAllUsers();
    messagePackage pack;
    pack.setType(messagePackage::AllUsers);
    pack.setAllUsers(users);
    messagePackage::sendMessage(&pack, socket);
}

// 创建群聊
void clientHandler::createGroup(QStringList users, QString groupName, QString sender)
{
    bool ret = datamanger::getInstance()->insertGroup(users, groupName);
    if (ret)
    {
        messagePackage pack;
        pack.setType(messagePackage::Group);
        pack.setRet(ret);
        pack.setGroupName(groupName);
        messagePackage::sendMessage(&pack, socket);
        emit addGroup(users, groupName, sender);

//        // 用户群聊列表更新
//        datamanger::getInstance()->insertUserGroupInfo(users, groupName);
    }
}

// 上传文件
void clientHandler::uploadFile(messagePackage info)
{
    QString fileName = info.sender() + "-" + info.fileName();
    QString filePath = "D:/work/qt/OnlineMeeting/MeetingServer/file/" + fileName;
    QString user = info.sender();
    //qDebug() << "filePath" << filePath;
    //qDebug() << "fileName" << fileName;
    //qDebug() << "sender" << user;

    bool ret = datamanger::getInstance()->insertFileInfo(filePath, fileName, user);

    messagePackage pack;
    pack.setType(messagePackage::UploadFile);
    pack.setRet(ret);
    messagePackage::sendMessage(&pack, socket);
    if (ret == false)
    {
        //qDebug() << "已存在同名文件";
        return ;
    }
    QFile file(filePath);
    file.open(QIODevice::WriteOnly);

    file.write(info.fileData());

    file.close();
}

// 查看所有文件
void clientHandler::fileinfo()
{
    QStringList files = datamanger::getInstance()->selectFileName();
    messagePackage pack;
    pack.setType(messagePackage::AllFile);
    pack.setAllFile(files);
    messagePackage::sendMessage(&pack, socket);
}

// 下载文件
void clientHandler::downlaodFile(QString fileName)
{
    QString filePath = datamanger::getInstance()->selectFilePath(fileName);
    if (filePath.isEmpty()) return ;
    //qDebug() << "filePath" << filePath;
    //qDebug() << "fileName" << fileName;
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly))
    {
        //qDebug() << "无法打开文件:" << filePath;
        return ;
    }
    QByteArray fileData = file.readAll();

    messagePackage pack;
    pack.setType(messagePackage::DownloadFile);
    pack.setFileData(fileData);
    pack.setFileName(fileName);
    messagePackage::sendMessage(&pack, socket);
}

